package com.example.chenchao.douyyin.utils;

import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class NetWorkUtil {
    private static OkHttpClient okHttpClient;
    public interface HttpCallBack{
        public void onFailure();
        public void onResponse(Response response);
    }
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;

    }


    public static void send(Map<String,String> params, final HttpCallBack httpCallBack){

        String str = JSON.toJSONString(params);
//        str = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
//        str = "JxVZ"+str+"Yny";
        //1.创建OkHttpClient对象
        if(okHttpClient==null) {
            okHttpClient = new OkHttpClient();
        }

        RequestBody body = new FormBody.Builder().add("a", str).build();
        //2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
        String statisticUrl = "http://127.0.0.1:83/saveClientVideoInfo";
        Request request = new Request.Builder().url(statisticUrl).post(body).build();

        //3.创建一个call对象,参数就是Request请求对象
        Call call = okHttpClient.newCall(request);
        //4.请求加入调度，重写回调方法
        call.enqueue(new Callback() {

            public void onFailure(Call call, IOException e) {
                Log.e("analysis error",e.toString());
                httpCallBack.onFailure();
            }

            public void onResponse(Call call, Response response) throws IOException {

                Log.e("analysis Response",response.toString());
                httpCallBack.onResponse(response);
            }
        });
    }
    public static void getBase64Str(final HttpCallBack httpCallBack){

//        String str = JSON.toJSONString(params);
//        str = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
//        str = "JxVZ"+str+"Yny";
        //1.创建OkHttpClient对象
        if(okHttpClient==null) {
            okHttpClient = new OkHttpClient();
        }

//        RequestBody body = new FormBody.Builder().build();
        //2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
        String statisticUrl = "http://127.0.0.1:83/getBase64Str";
        Request request = new Request.Builder().url(statisticUrl).get().build();

        //3.创建一个call对象,参数就是Request请求对象
        Call call = okHttpClient.newCall(request);
        //4.请求加入调度，重写回调方法
        call.enqueue(new Callback() {

            public void onFailure(Call call, IOException e) {
                Log.e("analysis error",e.toString());
                httpCallBack.onFailure();
            }

            public void onResponse(Call call, Response response) throws IOException {

                Log.e("analysis Response",response.body().string());
                httpCallBack.onResponse(response);
            }
        });
    }
    public static String escape(String src) {
        int i;
        char j;
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * 6);

        for (i = 0; i < src.length(); i++) {

            j = src.charAt(i);

            if (Character.isDigit(j) || Character.isLowerCase(j)
                    || Character.isUpperCase(j))
                tmp.append(j);
            else if (j < 256) {
                tmp.append("%");
                if (j < 16)
                    tmp.append("0");
                tmp.append(Integer.toString(j, 16));
            } else {
                tmp.append("%u");
                tmp.append(Integer.toString(j, 16));
            }
        }
        return tmp.toString();
    }
}
